"use client";
import { MenuDataItem } from "@ant-design/pro-layout";
import { DownCircleTwoTone } from "@ant-design/icons";
import ACCESS_ENUM from "@/access/accessEnum";

// 全局配置菜单列表
const menus = [
  {
    path: "/",
    name: "主页",
  },
  {
    path: "/banks",
    name: "题库",
  },
  {
    path: "/questions",
    name: "题目",
  },
  // {
  //   name: "参考页面",
  //   path: "https://mianshiya.com",
  //   target: "_blank",
  //   access: ACCESS_ENUM.USER,
  // },
  {
    name: "用户中心",
    path: "/user/center",
    // target: "_blank",
    access: ACCESS_ENUM.USER,
  },
  {
    path: "/admin",
    name: "管理",
    access: ACCESS_ENUM.ADMIN,
    icon: <DownCircleTwoTone />,
    children: [
      {
        path: "/admin/user",
        name: "用户管理",
        access: ACCESS_ENUM.ADMIN,
      },
      {
        path: "/admin/questionbank",
        name: "题库管理",
        access: ACCESS_ENUM.ADMIN,
      },
      {
        path: "/admin/question",
        name: "题目管理",
        access: ACCESS_ENUM.ADMIN,
      },
    ],
  },
] as MenuDataItem[];

// 导出
export default menus;

//  根据全部路径查找菜单
export const findAllMenuByPath = (path: string): MenuDataItem | null => {
  return findMenuByPath(menus, path);
};

//  根据路径查找菜单（使用递归）
export const findMenuByPath = (
  menus: MenuDataItem[],
  path: string,
): MenuDataItem | null => {
  for (const menu of menus) {
    if (menu.path === path) {
      return menu;
    }
    if (menu.children) {
      const matchedMenuItem = findMenuByPath(menu.children, path);
      if (matchedMenuItem) {
        return matchedMenuItem;
      }
    }
  }
  return null;
};
